/*
  Part of the MasterTool3D project - http://code.google.com/p/master-tool-3d

  Copyright (c) 2011 Ingo Pueschl

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation, version 2.1.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package techjit.gfx.core;

import java.util.Date;

import processing.core.PApplet;
import techjit.gfx.plugins.swe.SWE_Element;
import techjit.gfx.util.CoordinateSystemDrawer;
import techjit.gfx.util.PU;
import techjit.gfx.util.geo.Dimensions;
import techjit.gfx.util.geo.Vector;
import techjit.util.LogManager;
import techjit.util.RuntimeParams;

public abstract class GFX_Object {

	// FIELDS
	// head
	private int objectId;
	private static int nextObjectId = RuntimeParams.GFX__FIRST_ID;

	public Dimensions relDims = new Dimensions(1, 1.3f, 1.7f);

	// CONSTRUCTORS
	public GFX_Object() {
		// head
		objectId = nextObjectId++;
	}

	// METHODS
	public void changeRelativeDimensions(Dimensions relDims) {
		this.relDims.change(relDims);
	}

	protected void draw(PApplet p) {
		// head
		logDrawingInvocation(p);

		// bounding boxes
		draw_drawRelativeBoundingBox(p);

		// relative origin coordinate system
		draw_drawRelativeOriginCoordinateSystem(p);

	}

	protected boolean draw_determineDrawBounding() {
		if ((this instanceof GFX_Scene && !RuntimeParams.SHOW__SCENE_BOUNDING)
				|| (this instanceof GFX_Layout && !RuntimeParams.SHOW__LAYOUT_BOUNDING)
				|| (this instanceof SWE_Element && !RuntimeParams.SHOW__SWE_ELEMENT_BOUNDING)
				|| (this instanceof GFX_Spacer)) {
			return false;
		} else {
			return true;
		}
	}

	private void draw_drawRelativeBoundingBox(PApplet p) {
		p.pushStyle();
		{
			// scene bounding if relative box and show scene bounding
			if (this instanceof GFX_Scene) {
				if (RuntimeParams.SHOW__RELATIVE_BOUNDING_BOX
						&& RuntimeParams.SHOW__SCENE_BOUNDING) {
					draw_drawRelativeBoundingBox_draw(p);
				}
			}
			// for scene elements
			else {
				GFX_SceneElement sceneElement = (GFX_SceneElement) this;

				// for current if show current in any case with interval
				// parameter
				if (RuntimeParams.SHOW__RELATIVE_BOUNDING_BOX
						&& sceneElement.isCurrentSceneElement()
						&& RuntimeParams.SHOW__CURRENT) {
					if (((new Date()).getTime() % 1000) / 10 <= RuntimeParams.SHOW__BLINKING_INTERVAL) {
						draw_drawRelativeBoundingBox_draw(p);
					}
				}
				// if not show current if relative box and bounding for current
				// type
				else if (RuntimeParams.SHOW__RELATIVE_BOUNDING_BOX
						&& draw_determineDrawBounding()) {
					draw_drawRelativeBoundingBox_draw(p);
				}
			}
		}
		p.popStyle();
	}

	private void draw_drawRelativeBoundingBox_draw(PApplet p) {
		p.pushStyle();
		{
			p.noFill();
			if (this instanceof GFX_Scene) {
				p.strokeWeight(3);
				p.stroke(RuntimeParams.COLOR__DIRTY_GREEN);

			} else if (this instanceof GFX_Layout) {
				p.strokeWeight(2);
				p.stroke(RuntimeParams.COLOR__DIRTY_BLUE);
			} else {
				p.stroke(RuntimeParams.COLOR__DIRTY_RED);
			}
			PU.box(p, getRelativeDimensions(new Vector(1)));
		}
		p.popStyle();

	}

	private void draw_drawRelativeOriginCoordinateSystem(PApplet p) {
		if (RuntimeParams.SHOW__RELATIVE_COORDINATE_SYSTEMS
				&& draw_determineDrawBounding()) {
			float width, height, depth;
			if (relDims != null) {
				width = relDims.getWidth() + .5f;
				height = relDims.getHeight() + .5f;
				depth = relDims.getDepth() + .5f;
			} else {
				width = 1.5f;
				height = 1.5f;
				depth = 1.5f;
			}
			CoordinateSystemDrawer.draw(p, -.5f, -.5f, -.5f, width, height,
					depth);
		}
	}

	protected Vector getRelativeDimensions(Vector factor) {
		float factorX = factor.getX();
		float factorY = factor.getY();
		float factorZ = factor.getZ();

		float width = relDims.getWidth();
		float height = relDims.getHeight();
		float depth = relDims.getDepth();

		return new Vector(width * factorX, height * factorY, depth * factorZ);
	}

	protected void log() {
		LogManager.logALL("<GFX_Object " + objectId + ">");
		LogManager.indent();
		{
			// LogManager.logALL("relPos = " + relPos);
			LogManager.logALL("dimensions = " + relDims);
		}
		LogManager.deindent();
		LogManager.finishEntry();
	}

	protected void logAddElementsResult() {
		if (RuntimeParams.GFX_OBJECT__LOG_ADD_ELEMENTS_RESULT) {
			LogManager.logALL("Result of add element call:");
			LogManager.indent();
			{
				log();
			}
			LogManager.deindent();
		}
	}

	protected void logArrangementInvocation() {
		if (RuntimeParams.GFX_OBJECT__LOG_ARRANGEMENT_RESULT) {
			LogManager.logALL("Invoked arrange method:");
			LogManager.indent();
			{
				LogManager.logALL("" + this);
			}
			LogManager.deindent();
			LogManager.finishEntry();
		}
	}

	public void logConstructorResult() {
		if (RuntimeParams.GFX_OBJECT__LOG_CONSTRUCTOR_RESULT) {
			LogManager.logALL("Result of constructor call:");
			LogManager.indent();
			{
				log();
			}
			LogManager.deindent();
		}
	}

	protected void logDrawingInvocation(PApplet p) {
		if (RuntimeParams.GFX_OBJECT__LOG_DRAWING_RESULT) {
			LogManager.log(p, "Invoked drawing method:");
			LogManager.indent();
			{
				LogManager.log(p, "" + this);
			}
			LogManager.deindent();
			LogManager.finishEntry(p);
		}
	}

	protected void logLocalArrangementResult() {
		if (RuntimeParams.GFX_OBJECT__LOG_ARRANGEMENT_RESULT) {
			LogManager.logALL("Result of local arrange call:");
			LogManager.indent();
			{
				log();
			}
			LogManager.deindent();
		}
	}

}
